'use strict'

let models = require('../models');
let errors = require('../../config/errors');

let CheckTimeSet = models.CheckTimeSet;
let CheckTime = models.CheckTime;
let School = models.School;
let Grade = models.Grade;
let NatureClass = models.NatureClass;
let CheckTimeStatus = models.CheckTimeStatus;

let CheckTimeSetDao = models.CheckTimeSetDao;
let CheckTimeDao = models.CheckTimeDao;
let SchoolDao = models.SchoolDao;
let GradeDao = models.GradeDao;
let NatureClassDao = models.NatureClassDao;

let checkTimeService = require('./checktime');

function createInstance() {
    let checkTimeSet = new CheckTimeSet({
        weekdays: [
            checkTimeService.createInstance(),
            checkTimeService.createInstance(),
            checkTimeService.createInstance()
        ],
        saturday: [
            checkTimeService.createInstance(),
            checkTimeService.createInstance(),
            checkTimeService.createInstance()
        ],
        sunday: [
            checkTimeService.createInstance(),
            checkTimeService.createInstance(),
            checkTimeService.createInstance()
        ]
    });

    CheckTimeSetDao.create(checkTimeSet);

    return checkTimeSet;
}

let CheckTimeSetTypes = [ 'weekdays', 'saturday', 'sunday' ];

function updateCheckTimeSet(checkTimeSetId, checkTimeSetData) {
    let checkTimeSet = CheckTimeSetDao.findOne({
        id: checkTimeSetId
    });

    CheckTimeSetTypes.forEach(type => {
        if ( type in checkTimeSetData ) {
            updateCheckTimes(checkTimeSet, type, checkTimeSetData[type]);
        }
    });

    CheckTimeSetDao.update(checkTimeSet);

    return checkTimeSet;
}

function updateCheckTimes(checkTimeSet, type, checkTimes) {
    // 删除所有原有的时间
    checkTimeSet[type].forEach(checkTime => {
        CheckTimeDao.remove(checkTime);
    });

    // 生成新的时间
    checkTimeSet[type] = checkTimes.map(checkTimeData => {
        let checkTime = new CheckTime(checkTimeData);
        CheckTimeDao.create(checkTime);

        return checkTime;
    });
}

function getCheckTimeSetOfStudent(student) {
    let natureClass = NatureClassDao.findOne({
        students: student.id
    });
    
    let grade = GradeDao.findOne({
        natureClasses: natureClass.id
    });

    let school = SchoolDao.findOne({
        grades: grade.id
    });

    return combineCheckTimeSet(school.checkTimeSet, grade.checkTimeSet);
}

// 将学校与年级的考勤时间合并
function combineCheckTimeSet(schoolCheckTimeSet, gradeCheckTimeSet) {
    let checkTimeSet = {
        weekdays: schoolCheckTimeSet.weekdays,
        saturday: schoolCheckTimeSet.saturday,
        sunday: schoolCheckTimeSet.sunday
    };

    let days = Object.keys(checkTimeSet);
    days.forEach(dayName => {
        let isDayChecked = false;
        for ( let index in checkTimeSet[dayName] ) {
            if ( checkTimeSet[dayName][index].status == CheckTimeStatus.Unused ) {
                checkTimeSet[dayName][index] = gradeCheckTimeSet[dayName][index];
            }

            if ( checkTimeSet[dayName][index].status == CheckTimeStatus.Used ) {
                isDayChecked = true;
            }
        }

        if ( !isDayChecked ) {
            delete checkTimeSet[dayName];
        }
    });

    return checkTimeSet;
}

module.exports = {
    createInstance,
    updateCheckTimeSet,
    getCheckTimeSetOfStudent
};
